package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.base.enums.FlagEnum;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.dao.StudioPersonnelDao;
import com.ym.medical.domain.entity.DoctorDetailsEntity;
import com.ym.medical.domain.entity.DoctorStudioEntity;
import com.ym.medical.domain.entity.StudioPersonnelEntity;
import com.ym.medical.domain.param.StudioPersonnelParam;
import com.ym.medical.domain.param.StudioPersonnelUpdateParam;
import com.ym.medical.domain.vo.DoctorLinkVo;
import com.ym.medical.enums.CounsellingRoomCourTypeEnum;
import com.ym.medical.enums.StudioPersonnelRelEnum;
import com.ym.medical.enums.UserCertStateEnum;
import com.ym.medical.service.IDoctorDetailSV;
import com.ym.medical.service.IDoctorStudioSV;
import com.ym.medical.service.IStudioPersonnelSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 工作室成员Service业务层处理
 *
 * @author ym
 * @date 2020-08-21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StudioPersonnelSVImpl extends BaseSVImpl<StudioPersonnelEntity, StudioPersonnelDao> implements IStudioPersonnelSV {

	@Autowired
	@Lazy
	private IDoctorDetailSV iDoctorDetailSV;

	@Autowired
	private IDoctorStudioSV iDoctorStudioSV;

	@Override
	public List<Long> getSpecialistToGrabList(StudioPersonnelEntity studioPersonnel) {
		return mapper.getSpecialistToGrabList(studioPersonnel);
	}

	/**
	 * 工作室成员列表
	 *
	 * @param doctId
	 * @return
	 */
	@Override
	public List<StudioPersonnelEntity> selectPersons(Long doctId) {
		return mapper.selectPersons(doctId);
	}

	/**
	 * 全科工作室人员添加
	 * @param updateParam
	 * @return
	 */
	@Override
	public int addStudioPerson(StudioPersonnelUpdateParam updateParam) {
		//判断加入的医生是否是A类以下
		Long doctId = updateParam.getDoctId();
		DoctorDetailsEntity details01 = iDoctorDetailSV.selectById(doctId);
		if (Integer.valueOf(details01.getDoctType()) == 1
				&& CounsellingRoomCourTypeEnum.GENERAL.getKey().equals(details01.getDoctCategory())){
			throw new BusinessException("请选择 非A类 全科医生");
		}

		if (CounsellingRoomCourTypeEnum.SPECIALIZED_SUBJECT.getKey().equals(details01.getDoctCategory())){
			throw new BusinessException("不能操作专科医生");
		}

		Long dsId =  updateParam.getDsId();
		//判断A 类全科医生是否认证
		//1.1 查询工作室宿主id
		DoctorStudioEntity studio = iDoctorStudioSV.selectById(dsId);
		Long belong = studio.getBelongPerson();
		//判断被加入工作室宿主是否认证
		DoctorDetailsEntity details02 = iDoctorDetailSV.selectOne(
				TK.select(DoctorDetailsEntity.class).where()
				.andEqualTo(DoctorDetailsEntity::getId,belong)
				.andEqualTo(DoctorDetailsEntity::getQualificationStatus,UserCertStateEnum.CERTIFIED.getKey())
				.end()
		);
		if (details02 == null){
			throw new BusinessException("该工作室医生还未认证");
		}
		//判断重复添加
		StudioPersonnelParam model = StudioPersonnelParam.builder()
				.dsId(dsId).doctId(updateParam.getDoctId()).build();
		StudioPersonnelEntity personnel = this.selectOne(model.covert());
		if (personnel != null){
			throw new BusinessException("不能重复添加");
		}
		updateParam.setCreateTime(new Date());
		updateParam.setUpdateTime(new Date());
		updateParam.setAdmsStateDoctor(FlagEnum.IS.getKey());
		updateParam.setAdmsStateStudio(FlagEnum.IS.getKey());
		updateParam.setRelType(StudioPersonnelRelEnum.ORDINARY_MEMBERS.getKey());
		this.insert(updateParam.covert());
		return 1;
	}



	/**
	 * 全科医生工作室成员列表
	 * @param pageNo
	 * @param limit
	 * @param doctId
	 * @return
	 */
	@Override
	public PageInfo<DoctorLinkVo> studioPersonList(int pageNo, int limit, long doctId,String doctCategory) {
		PageHelper.startPage(pageNo,limit);
		String description = null;
		if (CounsellingRoomCourTypeEnum.GENERAL.getKey().equals(doctCategory)){
			description = "systemOpen";
		}
		List<DoctorLinkVo>  doctorLinkVos = mapper.studioPersonList(doctId,description);
		return new PageInfo<>(doctorLinkVos);
	}


}
